1
'****************************** Module Header ******************************'
2 ' Module Name: MainModule.vb
3 ' Project: VBSparseFile
4 ' Copyright (c) Microsoft Corporation.
6 ' VBSparseFile demonstrates the common operations on sparse files. A sparse
7 ' file is a type of computer file that attempts to use file system space more
8 ' efficiently when blocks allocated to the file are mostly empty. This is
9 ' achieved by writing brief information (metadata) representing the empty
10 ' blocks to disk instead of the actual "empty" space which makes up the
11 ' block, using less disk space. You can find in this example the creation of
12 ' sparse file, the detection of sparse attribute, the retrieval of sparse
13 ' file size, and the query of sparse file layout.
15 ' This source is subject to the Microsoft Public License.
16 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
17 ' All other rights reserved.
20 ' * 7/7/2009 11:34 PM Jialiang Ge Created
21 '***************************************************************************'
23 #Region
"Imports directives"
34 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
35 ' Determine if the volume support sparse streams.
38 If Not SparseFile
.VolumeSupportsSparseFiles("C:\") Then
39 Console
.WriteLine("Volume {0} does not support sparse streams", _
45 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
46 ' Create a sparse file.
49 Dim fileName
As String = "SparseFile.tmp"
50 Console
.WriteLine("Create sparse file: {0}", fileName
)
52 Using fs
As FileStream
= SparseFile
.Create(fileName
)
54 ' Write a large block of data
55 Const blockLength
As Integer = 512 * 1024 ' 512KB
56 Dim block
As Byte() = New Byte(blockLength
- 1) {}
57 For i
As Integer = 0 To blockLength
- 1
61 fs
.Write(block
, 0, blockLength
)
63 ' Set some sparse ranges in the block
65 SparseFile
.SetSparseRange(fs
.SafeFileHandle
, 0, &H10000
)
66 SparseFile
.SetSparseRange(fs
.SafeFileHandle
, &H20000
, &H20000
)
68 ' Set sparse block at the end of the file
70 ' 1GB sparse zeros are extended to the end of the file
71 fs
.SetLength(fs
.Length
+ &H40000000
)
76 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
77 ' Determine if a file is sparse.
80 Dim isSparse
As Boolean = SparseFile
.IsSparseFile(fileName
)
81 Console
.WriteLine("The file is{0} sparse", IIf(isSparse
, "", " not"))
84 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
88 SparseFile
.GetSparseFileSize(fileName
)
91 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
92 ' Query the sparse file layout.
95 SparseFile
.GetSparseRanges(fileName
)